#include"Lexer.h"
using namespace std;

Lexer::Lexer(const string & text)
{
    pos = Position(0,0,text);
    this->text = text;
    current_char = text[0];
}



void Lexer::advance()
{
    pos.advance(current_char);
}

void Lexer::make_tokens()
{
    /*
        遍历text获取token
    */
   while (current_char != '\0')
   {
       if((current_char==' ')||(current_char=='\t'))
       {
           //跳过空格与制表符
           this->advance();
       }
       else if(isdigit(current_char))
       {
           tokens.push_back(make_number());
       }
       else if(current_char == '+')
       {
           tokens.push_back(Token(TT_PLUS,""));
           this->advance();
       }
       else if(current_char == '-')
       {
           tokens.push_back(Token(TT_MINUS,""));
           this->advance();
       }
       else if(current_char == '*')
       {
           tokens.push_back(Token(TT_MUL,""));
           this->advance();
       }
       else if(current_char == '/')
       {
           tokens.push_back(Token(TT_DIV,""));
           this->advance();
       }
       else if(current_char == '(')
       {
           tokens.push_back(Token(TT_LPAREN,""));
           this->advance();
       }
       else if(current_char == ')')
       {
           tokens.push_back(Token(TT_RPAREN,""));
           this->advance();
       }
   }

}


Token Lexer::make_number()
{
    string num_str = "";
    while ((current_char != '\0')&&(isdigit(current_char)))
    {
        num_str += current_char;
        advance();
    }
    return Token(TT_INT,num_str);
}



vector<Token> Lexer::get_tokens()
{
    return this->tokens;
}
